package com.dandan.testcollection;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.dandan.testcollection.bean.NameBean;
import com.dandan.testcollection.httprequest.HttpApi;
import com.dandan.testcollection.httprequest.OnResponsListener;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.RecursiveAction;

public class RandomActivity extends AppCompatActivity {

    private Button random;
    private String url = "http://124.160.225.36:9999/football/oa/";
    private NameBean nameBean;
    private TextView textview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_random);


        httpRequest();

        random = findViewById(R.id.random);
        random.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(nameBean == null || nameBean.getFh() == null) {
                    httpRequest();
                    Toast.makeText(RandomActivity.this,"稍后再试",Toast.LENGTH_SHORT).show();
                    return;
                }
                int min = 0;
                int max = nameBean.getFh().size();
                Random random = new Random();
                int num = random.nextInt(max) % (max - min + 1) + min;
                textview.setText(num + "-----" +nameBean.getFh().get(num).getUsername());
            }
        });

        textview = findViewById(R.id.textview);

    }


    private void httpRequest() {
        List<String> param = new ArrayList<>();
        List<String> value = new ArrayList<>();
        param.add("username");
        value.add("fenghe");
        HttpApi.generalRequest(url, false, RandomActivity.this, param, value, new OnResponsListener() {
            @Override
            public void onSuccess(String body) {
                try {
                    Gson gson = new Gson();
                    nameBean = gson.fromJson(body, NameBean.class);


                } catch (Exception e) {

                }
            }

            @Override
            public void onField(String body) {
                System.out.println(body);
            }
        });
    }

}
